<?php
/**
 * @author chenlisong E-mail:chenlisong1021@163.com 
 * @version 创建时间：2015年8月19日 上午11:20:31 
 * 类说明
 */
class BaseAction extends Action{
	public static $uid;
	public function _initialize() {
		header("Content-type: text/html; charset=utf-8");
		$this->theme('default');
		if (!isset($_SESSION['USER_ADMIN_KEY_ID'])) {
			$this->redirect('Agents/Login/login');
		}
		if (!isset($_SESSION['USER_ADMIN_KEY_GROUP_ID'])) {
			$this->redirect('Agents/Login/login');
		}else{
			if($_SESSION['USER_ADMIN_KEY_GROUP_ID']!=2){
				echo '<p style="text-align:center;margin-top: 50px;font-size: 25px;">无权限操作!</p>';
				exit;
			}
		}
		//获取网站头
		$this->assign('edu_title',getFields('title'));
		//获取菜单树
		import('ORG.Util.Auth');//加载类库
		$auth=new Auth();
		$uid = $_SESSION['USER_ADMIN_KEY_ID'];
		$this->uid = $uid;
		$menuList = $auth->getAuthMenuList($uid);
		if(isset($_SESSION['ADMIN_MENU_LIST'.$uid])){
			$mainMenu = $_SESSION['ADMIN_MENU_LIST'.$uid];
		}else{
			$menuList = $auth->getAuthMenuList($uid);
			//主菜单
			$i = 0;
			foreach($menuList as $key=>$val){
				if($val['parent_id'] == 0){
					$mainMenu[$i] = $menuList[$key];
					foreach($menuList as $key2=>$val2){
						if($val2['parent_id'] == $val['id']){
							$mainMenu[$i]['subMenu'][] = $val2;
						}
					}
					$i++;
				}
			}
			$_SESSION['ADMIN_MENU_LIST'.$uid] = $mainMenu;
		}
		$this->assign('menuList',$mainMenu);
		//超级管理员
		if(in_array(session('USER_ADMIN_KEY_ID'), C('ADMINISTRATOR'))){
			return true;
		}
		//过滤不需要验证的模块
		if(in_array(MODULE_NAME, C('NOT_AUTH_MODULE')) || in_array(ACTION_NAME, C('NOT_AUTH_ACTION'))){
			return true;
		}elseif(!$auth->check('Admin/' . MODULE_NAME.'/index',session('USER_ADMIN_KEY_ID'))){
			echo '<p style="text-align:center;margin-top: 50px;font-size: 25px;">无权限操作!</p>';
			exit;
        }
	}

	/**
	 * @authority 新增
	 */
	public function add(){
		$this->display('add');
	}

	/**
	 * 插入
	 */
	public function insert(){
		$Model = M($this->getActionName());
		unset ( $_POST [$Model->getPk()] );
		if (false === $Model->create()) {
			$this->error($Model->getError());
		}
		//保存当前数据对象
		if ($result = $Model->add()) { //保存成功
			$this->ajaxReturn(1,L('ADMIN_ADD_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_ADD_ERROR'),0);
		}
	}
	
	/**
	 * @authority 修改
	 */
	public function edit() {
		$Model = M($this->getActionName());
		$map[$Model->getPk()] = $_REQUEST['id'];
		$vo = $Model->where($map)->find();
		$this->assign('vo',$vo);
		$this->display();
	}
	
	/**
	 * 更新
	 */
	public function update() {
		$Model = M($this->getActionName());
		if(false === $Model->create()) {
			$this->error($Model->getError());
		}
		// 更新数据
		if(false !== $Model->save()) {
			$this->ajaxReturn(1,L('ADMIN_EDIT_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
		}
	}
	
	/**
	 * @authority 详细
	 */
	public function detailView() {
		$Model = M($this->getActionName());
		$map[$Model->getPk()] = $_REQUEST['id'];
		$vo = $Model->where($map)->find();
		$this->assign('vo',$vo);
		$this->display();
	}
	
	
	/**
	 * 逻辑删除
	 */
	public function logicDel(){
		$Model = M($this->getActionName());
		if (!empty($Model)) {
			$pk = $Model->getPk();
			$id = $_REQUEST[$pk];
			if (isset($id)) {
				$condition = array($pk => array('in', explode(',', $id)));
				if (false !== $Model->where($condition)->setField('flag',2)) {
					$this->ajaxReturn(1,L('ADMIN_DELETE_SUCCESS'),1);
				} else {
					$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
				}
			} else {
				$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
			}
		}
	}
	/**
	 * 物理删除
	 */
	public function del(){
		$Model = M($this->getActionName());
		if (!empty($Model)) {
			$pk = $Model->getPk();
			$id = $_REQUEST[$pk];
			if (isset($id)) {
				$condition = array($pk => array('in', explode(',', $id)));
				if (false !== $Model->where($condition)->delete()) {
					$this->ajaxReturn(1,L('ADMIN_DELETE_SUCCESS'),1);
				} else {
					$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
				}
			} else {
				$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
			}
		}
	}
	
	/**
	 * 更改状态
	 */
	public function updateStatus() {
		//删除指定记录
		$Model = M($this->getActionName());
		$pk = $Model->getPk();
		$id = $_REQUEST[$pk];
		if (isset($id)) {
			$condition = array($pk => array('in', explode(',', $id)));
			if (false !== $Model->where($condition)->setField($_REQUEST['field'],$_REQUEST['islock'])) {
				$this->ajaxReturn(1,L('ADMIN_EDIT_SUCCESS'),1);
			} else {
				$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
			}
		}
	}
	
	/**
	 * 封装dtgird的json数据
	 */
	public function toJosnForGird($data,$page){
		if(empty($page)){
			$page->page = 0;
			$page->total = 0;
			$page->records = 0;
		}
		$newdata['page'] = $page->page;
		$newdata['total'] = $page->total;
		$newdata['records'] = $page->records;
		$newdata['rows'] = $data;
		return $newdata;
	}
	
	public function pageParam($totalRows = 0){
		if(empty($totalRows)){
			return false;
		}
		$records = $totalRows;//总条数
		$listRows = empty($_GET['rows'])?20:$_GET['rows'];
		if(!empty($listRows)) {
			$listRows =   intval($listRows);
		}
		//排序
		$sidx = empty($_GET['sidx'])?M($this->getActionName())->getPk():$_GET['sidx'];
		$sord = empty($_GET['sord'])?"desc":$_GET['sord'];
		$sort = $sidx." ".$sord;
		$totalPages   =   ceil($totalRows/$listRows);     
		$nowPage      =   !empty($_GET['page'])?intval($_GET['page']):1;
		$firstRow     =   $listRows*($nowPage-1);
		$data['total'] = $totalPages; //总页数
		$data['page'] = $nowPage;//当前页数
		$data['records'] = $totalRows;//总数量
		$data['listRows'] = $listRows;//获取默认列表每页显示行数,默认20
		$data['firstRow'] = $firstRow;//当前页数-1
		$data['sort'] = $sort;
		return json_encode($data);
	}
}